﻿/// <reference path="typings/swiper/swiper.d.ts" />
/// <reference path="typings/jquery/jquery.d.ts" />
/// <reference path="typings/knockout.mapping/knockout.mapping.d.ts" />
/// <reference path="ecatalog.api.ts" />
var Ecatalog;
(function (Ecatalog) {
    var SizeViewModel = (function () {
        function SizeViewModel(sizeDto, selectedSizes) {
            var _this = this;
            this.selectedSizes = selectedSizes;
            this.size = new Ecatalog.Api.SizeDto();
            this.selected = ko.observable(false);
            this.size = sizeDto;
            var selectedSizes = this.selectedSizes();
            for (var i = 0; i < selectedSizes.length; i++) {
                var isModel = sizeDto.ModelId == selectedSizes[i].ModelId;
                var isSize = sizeDto.Value == selectedSizes[i].Value;
                if (isModel && isSize) {
                    this.selected(true);
                }
            }

            this.select = function (data) {
                var selected = _this.selected();
                if (!selected) {
                    _this.selectedSizes.push(data.size);
                } else {
                    var idx = _this.selectedSizes.indexOf(data.size);
                    if (idx != -1) {
                        _this.selectedSizes.splice(idx, 1);
                    }
                }
                _this.selected(!selected);
            };
        }
        return SizeViewModel;
    })();
    Ecatalog.SizeViewModel = SizeViewModel;

    var Catalog = (function () {
        function Catalog(target) {
            var _this = this;
            this.kits = ko.observableArray([]);
            this.models = ko.observableArray([]);
            this.structures = ko.observableArray([]);
            this.colors = ko.observableArray([]);
            this.selectedSizes = ko.observableArray([]);
            this.sizes = ko.observableArray([]);
            this.description = ko.observable("");
            this.kitImageUrl = ko.observable("");
            this.shopMapUrl = ko.observable("");
            this.locationModel = ko.observable(null);
            this.locationComplect = ko.observableArray([]);
            this.costComplect = ko.observable(0);
            this.discountComplect = ko.observable(0);
            this.notSizeShopId = ko.observable(0);
            this.notSizeModelId = ko.observable(0);
            this.notifySize = ko.observable("");
            this.showLocComplect = ko.observable(true);
            this.activeModelId = ko.observable(0);
            this.kitSlidesPerView = 1;
            this.setKitSlidesPerView = function (count) {
                if (count < 3) {
                    _this.kitSlidesPerView = 1;
                } else {
                    _this.kitSlidesPerView = 3;
                }
            };

            this.setNotifySize = function (value) {
                _this.notifySize(value);
            };

            this.showModelLocation = function () {
                _this.showLocComplect(false);
                _this.refresh();
            };

            this.showComplectLocation = function () {
                _this.showLocComplect(true);
                _this.refresh();
            };

            this.refresh = function () {
                var locations = $("[name*='location-']");
                $.each(locations, function () {
                    var x = $(this).data("x");
                    var y = $(this).data("y");
                    $(this).css({
                        left: x + 'px',
                        top: y + 'px'
                    });
                });
            };

            this.init = function () {
                var self = _this;

                function SetKitData(data) {
                    var active = data.slides[data.activeIndex];
                    var imageUrl = $(active).attr("data-kitImage");
                    var costComplect = parseFloat($(active).attr("data-costComplect"));
                    var discountComplect = parseFloat($(active).attr("data-discountComplect"));
                    var kitid = parseInt($(active).attr("data-kitId"));

                    self.kitImageUrl(imageUrl);
                    self.costComplect(costComplect);
                    self.discountComplect(discountComplect);
                    self.loadKitData(kitid);
                }

                if (_this.kitSlidesPerView == 1) {
                    var kitSwiper = new Swiper('#swiper-container-kitmodels', {
                        pagination: '.pagination-kit',
                        paginationClickable: false,
                        grabCursor: true,
                        slidesPerView: 1,
                        loop: false,
                        onTouchEnd: function (data) {
                            $('.wall').hide();
                            var active = data.slides[data.activeIndex];
                            var kitid = parseInt($(active).attr("data-kitId"));
                            self.kitImageUrl($(active).attr("data-kitImage"));
                            self.loadKitData(kitid);
                            self.setActiveModels(kitid);
                        },
                        onImagesReady: function (data) {
                            SetKitData(data);
                        }
                    });
                } else if (_this.kitSlidesPerView == 3) {
                    var kitSwiper = new Swiper('#swiper-container-kitmodels', {
                        pagination: '.pagination-kit',
                        paginationClickable: false,
                        grabCursor: true,
                        slidesPerView: 3,
                        loop: false,
                        centeredSlides: true,
                        watchActiveIndex: true,
                        onTouchEnd: function (data) {
                            $('.wall').hide();
                            var active = data.slides[data.activeIndex];
                            var kitid = parseInt($(active).attr("data-kitId"));
                            self.kitImageUrl($(active).attr("data-kitImage"));
                            self.loadKitData(kitid);
                            self.setActiveModels(kitid);
                        },
                        onImagesReady: function (data) {
                            SetKitData(data);
                        }
                    });
                }

                $(".swiper-kitmodels").each(function (index, elem) {
                    var count = parseInt($(elem).attr("data-count"));
                    var pag = '.p-' + $(elem).attr("id");

                    if (count < 3) {
                        var mySwiper = new Swiper($(this)[0], {
                            pagination: pag,
                            paginationClickable: false,
                            grabCursor: true,
                            slidesPerView: 1,
                            loop: false,
                            onTouchEnd: function (data) {
                                var active = data.slides[data.activeIndex];
                                var modelId = parseInt($(active).attr("data-modelId"));
                                self.loadModelData(modelId);
                            },
                            onImagesReady: function (data) {
                                var active = data.slides[data.activeIndex];
                                for (var i = 0; i < data.slides.length; i++) {
                                    var modelId = parseInt($(data.slides[i]).attr("data-modelId"));
                                    if (self.activeModelId() == modelId) {
                                        self.notSizeModelId(modelId);
                                        mySwiper.swipeTo(i - 1);
                                        self.loadModelData(modelId);
                                        break;
                                    }
                                    ;
                                }
                            }
                        });
                    } else {
                        var mySwiper = new Swiper($(this)[0], {
                            pagination: pag,
                            paginationClickable: false,
                            grabCursor: true,
                            slidesPerView: 3,
                            loop: false,
                            centeredSlides: true,
                            watchActiveIndex: true,
                            onTouchEnd: function (data) {
                                var active = data.slides[data.activeIndex];
                                var modelId = parseInt($(active).attr("data-modelId"));
                                self.loadModelData(modelId);
                            },
                            onImagesReady: function (data) {
                                var active = data.slides[data.activeIndex];
                                for (var i = 0; i < data.slides.length; i++) {
                                    var modelId = parseInt($(data.slides[i]).attr("data-modelId"));
                                    if (self.activeModelId() == modelId) {
                                        self.notSizeModelId(modelId);
                                        mySwiper.swipeTo(i - 1);
                                        self.loadModelData(modelId);
                                        break;
                                    }
                                    ;
                                }
                            }
                        });
                    }
                });
            };

            this.setModels = function (kitDtos) {
                for (var i = 0; i < kitDtos.length; i++) {
                    for (var j = 0; j < kitDtos[i].ModelDtos.length; j++) {
                        _this.models.push(kitDtos[i].ModelDtos[j]);
                    }
                }
            };

            this.setActiveModels = function (activeKitId) {
                $(".swiper-kitmodels").hide();
                $(".pagination-kit-models").hide();
                var selector = "kit-" + activeKitId;
                $("#" + selector).show();
                $(".p-" + selector).show();

                //получаю id отображаемой модели
                var modelId = $("#kit-" + activeKitId).find(".swiper-wrapper").find(".swiper-slide-active").attr("data-modelId");
                _this.loadModelData(parseInt(modelId));
            };

            this.loadModelData = function (modelId) {
                var m = _this.models();
                for (var i = 0; i < m.length; i++) {
                    if (m[i].ModelId == modelId) {
                        _this.notSizeModelId(m[i].ModelId);
                        _this.structures(m[i].StructuresDtos);
                        _this.colors(m[i].ColorsDtos);
                        var arr = [];
                        var ms = m[i].SizeDtos;
                        for (var j = 0; j < ms.length; j++) {
                            var vm = new SizeViewModel(ms[j], _this.selectedSizes);
                            arr.push(vm);
                        }
                        _this.sizes(arr);
                        _this.description(m[i].Description);
                        _this.locationModel(m[i].LocationDto);
                        _this.refresh();
                        break;
                    }
                }
            };

            this.loadKitData = function (kitid) {
                var k = _this.kits();
                for (var i = 0; i < k.length; i++) {
                    if (k[i].KitId == kitid) {
                        _this.locationComplect(k[i].LocationDtos);
                        _this.discountComplect(k[i].DiscountComplect);
                        _this.costComplect(k[i].CostComplect);
                        _this.refresh();
                        break;
                    }
                }
            };

            this.loadKits = function (organizationId, numModel) {
                var params = new Ecatalog.Api.GetComplectsParams();
                params.NumModel = numModel;
                params.OrganizationId = organizationId;
                Ecatalog.Api.AsyncClient.Current.GetComplects(params, function (result) {
                    if (result != null) {
                        _this.setKitSlidesPerView(result.KitDtos.length);
                        _this.activeModelId(result.ActiveModelId);
                        _this.notSizeModelId(result.ActiveModelId);
                        _this.notSizeShopId(result.ShopId);
                        _this.kits(result.KitDtos);
                        _this.setModels(result.KitDtos);
                        _this.shopMapUrl(result.ShopMapUrl);
                        _this.init();
                        var selector = "kit-" + result.ActiveKitId;
                        $(".pagination-kit-models").hide();
                        $("#" + selector).show();
                        $(".p-" + selector).show();
                    }
                });
            };
            ko.applyBindings(this, target);
        }
        return Catalog;
    })();
    Ecatalog.Catalog = Catalog;
})(Ecatalog || (Ecatalog = {}));

$(document).ready(function () {
    var catalogElem = $("[data-catalog='true']");
    if (catalogElem.length > 0) {
        var ecatalog = new Ecatalog.Catalog(catalogElem[0]);
        var organizationId = $("#OrganizationId").val();
        var numModel = $("#NumModel").val();
        ecatalog.loadKits(organizationId, numModel);
    }
});
//# sourceMappingURL=ecatalog.js.map
